package icbc.nav.view
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import icbc.app.AppGlobal;
	import icbc.app.AppText;
	import icbc.app.control.Alert;
	import icbc.app.control.CoolButton;
	import icbc.app.util.GlideTransition;
	import icbc.nav.Session;
	import icbc.nav.control.BotMenuControl;
	import icbc.nav.control.MenuControl;
	import icbc.nav.control.MenuListControl;
	import icbc.nav.entity.Menu;
	import icbc.nav.entity.MenuChild;
	import icbc.nav.event.MenuContentEvent;
	import icbc.nav.event.ViewEvent;
	
	public class MenuView extends View
	{
		private var backgroundImage:String = null;
		private var image:Sprite;
		private var menuControl:MenuControl;
		private var contentView:ContentView;
		private var botMenu:BotMenuControl;
		
		public function MenuView()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			loadBackground();
			initContent();
		}
		
		private function initContent():void
		{
			var panel:Sprite = setBasePanel();
			addChild(panel);
			panel.x = stage.stageWidth/2 - panel.width/2;
			panel.y = stage.stageHeight/2 - panel.height/2;

			menuControl = new MenuControl(Session.instance().menu);
			menuControl.onShowMenu.add(menuShowHandler);
			menuControl.onHideMenu.add(menuHideHandler);
			menuControl.addEventListener(MenuContentEvent.OPEN, openContentHandler);
			panel.addChild(menuControl);
			
			botMenu = new BotMenuControl(Session.instance().menu.childs);
			botMenu.onClick.add(menuClickHandler);
			addChild(botMenu);
			botMenu.x = 20;
			botMenu.y = AppGlobal.DEFAULT_HEIGHT - botMenu.height;
			botMenu.visible = false;
		}
		
		private function menuClickHandler(name:String):void
		{
			navigateToMenu(name);
		}
		
		private function navigateToMenu(name:String):void
		{
			menuControl.navigateToRootMenu(name);
			if(contentView != null)
			{
				removeContentListener();
				if(contentView.parent != null)
				{
					contentView.dispose();
				}
			}
		}
		
		private function menuShowHandler(menuNro:int):void
		{
			botMenu.visible = true;
			botMenu.setMenuState(menuNro);
		}
		
		private function menuHideHandler():void
		{
			botMenu.visible = false;
			botMenu.clearMenuState();
		}
		
		private function openContentHandler(evt:MenuContentEvent):void
		{
			removeContentListener();
			openContent(evt.data);
		}
		
		private function openContent(menuChild:MenuChild):void
		{
			contentView = new ContentView(menuChild);
			contentView.onTopLink.add(topLinkHandler);
			contentView.onNavClick.add(navClickHandler);
			contentView.onNavHomeClick.add(navHomeClickHandler);
			contentView.onReady.add(contentReadyHandler);
			contentView.addEventListener(ContentView.EVENT_HOME, contentHomeHandler);
			contentView.addEventListener(MenuContentEvent.OPEN, openContentHandler);
			addChild(contentView);
			
			swapChildren(contentView, botMenu);
		}
		
		private function contentReadyHandler():void
		{
			menuControl.visible = true;
			TweenLite.to(menuControl, 2, {alpha:1});
		}
		
		private function topLinkHandler(menuName:String, fullPath:Array):void
		{
			goToMenuName(menuName, fullPath);
		}
		
		private function removeContentListener():void
		{
			if(contentView == null) return;
			contentView.removeEventListener(ContentView.EVENT_HOME, contentHomeHandler);
			contentView.removeEventListener(MenuContentEvent.OPEN, openContentHandler);
		}
		
		private function contentHomeHandler(evt:Event):void
		{
			if(contentView != null) contentView.dispose();
			removeContentListener();
			menuControl.closeChild();
		}
		
		private function goToMenuName(menuName:String, fullPath:Array):void
		{
			contentView.dispose();
			return;
			menuControl.closeChild();
			menuControl.navigateTo(menuName, fullPath);
		}
		
		private function navClickHandler(menuChild:MenuChild):void
		{
			removeContentListener();
			menuControl.visible = false;
			menuControl.alpha = 0;
			TweenLite.to(contentView, 1, {
				alpha:0, ease:Strong.easeInOut,
				onComplete:function():void{
					contentEndHanlder(menuChild);
				}
			});
		}
		
		private function navHomeClickHandler():void
		{
			navigateToMenu(AppText.MENU_HOME);
		}
		
		private function contentEndHanlder(menuChild:MenuChild):void
		{
			contentView.dispose();
			openContent(menuChild);
		}

		private function loadBackground():void
		{
			image = new Sprite();
			addChild(image);

			backgroundImage = AppGlobal.PATH_BACKGROUND;
			if(backgroundImage == null)
				return;
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.load(new URLRequest(AppGlobal.combinePath(backgroundImage)));
			image.addChild(loader);
		}
		
		private function onIOError(evt:IOErrorEvent):void
		{
			trace("MenuView.Image["+ backgroundImage +"].IOError: "+ evt.text);
		}		
		
		private function imageComplete(evt:Event):void
		{
			var image:DisplayObject = (evt.target as LoaderInfo).content;
		}
	}
}